
//package java_cup;

/**
 * This class represents the complete "reduce-goto" table of the CUPParser. It has
 * one row for each state in the parse machines, and a column for each terminal
 * SymbolDraft. Each entry contains a state Count to Shift to as the last step of a
 * reduce.
 *
 * @see java_cup.parse_reduce_row
 * @version last updated: 11/25/95
 * @author Scott Hudson
 */
public class ParseReduceTable {

    /*-----------------------------------------------------------*/
    /*--- Constructor(s) ----------------------------------------*/
    /*-----------------------------------------------------------*/

    /**
     * Simple constructor. Note: AllVns terminals, non-terminals, and AllProdctions must
     * already have been entered, and the viable prefix recognizer should have been
     * constructed before this is called.
     */
    public ParseReduceTable() {
        /* determine how many states we are working with */
        _num_states = LALRState.Count;

        /* allocate the array and fill it in with empty rows */
        under_state = new ParseReduceRow[_num_states];
        for (int i = 0; i < _num_states; i++)
            under_state[i] = new ParseReduceRow();
    }

    /*-----------------------------------------------------------*/
    /*--- (Access to) Instance Variables ------------------------*/
    /*-----------------------------------------------------------*/

    /** How many rows/states in the machine/table. */
    public int _num_states;

    /** How many rows/states in the machine/table. */
    public int Count => _num_states;

    /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */

    /** Actual array of rows, one per state */
    public ParseReduceRow[] under_state;

    /*-----------------------------------------------------------*/
    /*--- General Methods ---------------------------------------*/
    /*-----------------------------------------------------------*/

    /** Convert to a string. */
    /*@Override*/
    public override string ToString() {
        String result;
        LALRState goto_st;
        int cnt;

        result = "-------- REDUCE_TABLE --------\n";
        for (int row = 0; row < Count; row++) {
            result += "From state #" + row + "\n";
            cnt = 0;
            for (int col = 0; col < ParseReduceRow.ColumnCount; col++) {
                /* pull @out the table entry */
                goto_st = under_state[row].under_non_term[col];

                /* if it has actionPart in it, print it */
                if (goto_st != null) {
                    result += " [non term " + col + "->";
                    result += "state " + goto_st.Index + "]";

                    /* end the line after the 3rd one */
                    cnt++;
                    if (cnt == 3) {
                        result += "\n";
                        cnt = 0;
                    }
                }
            }
            /* finish the line if we haven't just done that */
            if (cnt != 0)
                result += "\n";
        }
        result += "-----------------------------";

        return result;
    }

    /*-----------------------------------------------------------*/

}
